clear all;clc
addpath(genpath('.'));  %添加工具箱路径
%% 如下是空域图像
% load ./'Color Sets'/C1.mat
% C_SPATIAL = double(C1(:,:,2));
C_SPATIAL = double(imread('./Image Sets/lena.pgm'));
%% 如下是JPEG图像
%  C_SPATIAL = double(imread('./JPEG Sets/lena.jpg'));
%  C_STRUCT = jpeg_read('./JPEG Sets/lena.jpg');
[width,height]=size(C_SPATIAL);
stego_cover=zeros(width,height);
cover=C_SPATIAL;
global SUM
global preform 
preform=[];
SUM=0;
%% 显著性计算--算法：salient_LC
sql_map=salient_LC(C_SPATIAL); %代价1

%% 失真代价计算--算法：J_UNIWARD
% [M,T_SUM]=cal_M_J(C_SPATIAL,C_STRUCT,sql_map);  %代价2，SUM为M中的点数
%% 失真代价计算--算法：S_UNIWARD &WOW
[M,T_SUM]=cal_M_S(C_SPATIAL,sql_map);  %代价2，SUM为M中的点数
SUM;
%% 设置4*4分块的评判矩阵mark_block
N=4; %%暂定分块大小为4
%a必须小于b
% a=1/8;
% b=1/4;

for a=0:1:16
    for b=0:1:16
        Estimatefra(M,N,a,b);%暴力估计
    end
end
[x,y]=Select_M(preform);
mark_block = Estimatefra(M,N,x,y);%M是修改矩阵，N为分块大小
[w_m,h_m]=size(mark_block);
gray2color(mark_block)
% mark_block=2-mark_block;

%% msg生成
key=rng; %%key为伪随机序列种子
msg = num2str(uint8(rand(1,width*height)));
msg=strrep(msg,' ','');%剔除空格
p=1;%指向msg的指针
%% 设定分块策略
tic
for i=1:w_m
    for j=1:h_m
        if mark_block(i,j)==2
            %% 1*1分块策略——非直接LSB嵌入
            [emd_msg,p]=get_msg(msg,p,16);%可嵌入16bit
            stego_cover((i-1)*N+1:i*N,(j-1)*N+1:j*N)=indirect_LSB_2(cover((i-1)*N+1:i*N,(j-1)*N+1:j*N),emd_msg);
        end
        if mark_block(i,j)==1
            %% 2*2分块策略——反位调制
            [emd_msg,p]=get_msg(msg,p,8);%可嵌入8bit
            stego_cover((i-1)*N+1:i*N,(j-1)*N+1:j*N)=SPA_Remod(cover((i-1)*N+1:i*N,(j-1)*N+1:j*N),emd_msg);
%             stego_cover((i-1)*N+1:i*N,(j-1)*N+1:j*N)=DCT_MED_LSB(cover((i-1)*N+1:i*N,(j-1)*N+1:j*N),emd_msg);
%             stego_cover((i-1)*N+1:i*N,(j-1)*N+1:j*N)=cover((i-1)*N+1:i*N,(j-1)*N+1:j*N);
        end
        if mark_block(i,j)==0
            %% 4*4分块策略——DCT高频+QIM量化
            % 修改了哪些地方：嵌入量减少，P值下来了
            [emd_msg,p]=get_msg(msg,p,9);%可嵌入9bit
            delta=3;%暂时设置量化步长为1;
            stego_cover((i-1)*N+1:i*N,(j-1)*N+1:j*N)=DCT_HIG_LSB(cover((i-1)*N+1:i*N,(j-1)*N+1:j*N),emd_msg,delta);
        end
    end
end
toc

stego_cover=round(stego_cover);
PSNR=find_psnr(C_SPATIAL,double(stego_cover))
SSIM1=find_ssim(C_SPATIAL,double(stego_cover))
SSIM2=ssim(C_SPATIAL,double(stego_cover))
save stego_cover stego_cover
save mark_block mark_block
save key key
save p p
save msg msg
